const Router = require('@koa/router');
const deviceController = require('../controllers/device.controller');
const { checkAuth, checkPermission } = require('../middlewares/auth');

const router = new Router({
  prefix: '/api/v1/devices'
});

/**
 * @swagger
 * /api/v1/devices:
 *   get:
 *     tags: [设备管理]
 *     summary: 获取设备列表
 *     description: 获取所有设备的列表（支持分页和筛选）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [sensor, controller, gateway]
 *         description: 设备类型筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [online, offline, fault]
 *         description: 设备状态筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 100
 *                 pages:
 *                   type: integer
 *                   example: 10
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 devices:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "dev_001"
 *                       name:
 *                         type: string
 *                         example: "温湿度传感器-01"
 *                       type:
 *                         type: string
 *                         example: "sensor"
 *                       status:
 *                         type: string
 *                         example: "online"
 *                       location:
 *                         type: string
 *                         example: "3号楼-501"
 *                       lastActiveTime:
 *                         type: string
 *                         format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission(['device:read']), deviceController.getDevices);

/**
 * @swagger
 * /api/v1/devices/{id}:
 *   get:
 *     tags: [设备管理]
 *     summary: 获取设备详情
 *     description: 获取指定设备的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "dev_001"
 *                 name:
 *                   type: string
 *                   example: "温湿度传感器-01"
 *                 type:
 *                   type: string
 *                   example: "sensor"
 *                 model:
 *                   type: string
 *                   example: "DHT22"
 *                 manufacturer:
 *                   type: string
 *                   example: "科技公司"
 *                 serialNumber:
 *                   type: string
 *                   example: "SN2024001"
 *                 firmwareVersion:
 *                   type: string
 *                   example: "v1.2.0"
 *                 status:
 *                   type: string
 *                   example: "online"
 *                 location:
 *                   type: string
 *                   example: "3号楼-501"
 *                 ipAddress:
 *                   type: string
 *                   example: "192.168.1.100"
 *                 macAddress:
 *                   type: string
 *                   example: "00:11:22:33:44:55"
 *                 lastActiveTime:
 *                   type: string
 *                   format: date-time
 *                 config:
 *                   type: object
 *                   description: 设备配置信息
 *                 metadata:
 *                   type: object
 *                   description: 设备元数据
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 */
router.get('/:id', checkAuth, checkPermission(['device:read']), deviceController.getDevice);

/**
 * @swagger
 * /api/v1/devices:
 *   post:
 *     tags: [设备管理]
 *     summary: 创建设备
 *     description: 添加新的设备到系统
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *               - model
 *             properties:
 *               name:
 *                 type: string
 *                 description: 设备名称
 *                 example: "温湿度传感器-01"
 *               type:
 *                 type: string
 *                 enum: [sensor, controller, gateway]
 *                 description: 设备类型
 *                 example: "sensor"
 *               model:
 *                 type: string
 *                 description: 设备型号
 *                 example: "DHT22"
 *               manufacturer:
 *                 type: string
 *                 description: 制造商
 *                 example: "科技公司"
 *               serialNumber:
 *                 type: string
 *                 description: 序列号
 *                 example: "SN2024001"
 *               location:
 *                 type: string
 *                 description: 安装位置
 *                 example: "3号楼-501"
 *               config:
 *                 type: object
 *                 description: 设备配置信息
 *               metadata:
 *                 type: object
 *                 description: 设备元数据
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "dev_001"
 *                 message:
 *                   type: string
 *                   example: "设备创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/', checkAuth, checkPermission(['device:create']), deviceController.createDevice);

/**
 * @swagger
 * /api/v1/devices/{id}:
 *   put:
 *     tags: [设备管理]
 *     summary: 更新设备
 *     description: 更新指定设备的信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 设备名称
 *                 example: "温湿度传感器-01"
 *               location:
 *                 type: string
 *                 description: 安装位置
 *                 example: "3号楼-501"
 *               config:
 *                 type: object
 *                 description: 设备配置信息
 *               metadata:
 *                 type: object
 *                 description: 设备元数据
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "设备更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 */
router.put('/:id', checkAuth, checkPermission(['device:update']), deviceController.updateDevice);

/**
 * @swagger
 * /api/v1/devices/{id}:
 *   delete:
 *     tags: [设备管理]
 *     summary: 删除设备
 *     description: 从系统中删除指定的设备
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "设备删除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 */
router.delete('/:id', checkAuth, checkPermission(['device:delete']), deviceController.deleteDevice);

/**
 * @swagger
 * /api/v1/devices/{id}/bind:
 *   post:
 *     tags: [设备管理]
 *     summary: 绑定设备
 *     description: 将设备绑定到用户或位置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - bindType
 *               - bindId
 *             properties:
 *               bindType:
 *                 type: string
 *                 enum: [user, location]
 *                 description: 绑定类型
 *                 example: "location"
 *               bindId:
 *                 type: string
 *                 description: 绑定目标ID
 *                 example: "loc_501"
 *     responses:
 *       200:
 *         description: 绑定成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "设备绑定成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 */
router.post('/:id/bind', checkAuth, checkPermission(['device:bind']), deviceController.bindDevice);

/**
 * @swagger
 * /api/v1/devices/{id}/unbind:
 *   post:
 *     tags: [设备管理]
 *     summary: 解绑设备
 *     description: 解除设备的绑定关系
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     responses:
 *       200:
 *         description: 解绑成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "设备解绑成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 */
router.post('/:id/unbind', checkAuth, checkPermission(['device:bind']), deviceController.unbindDevice);

/**
 * @swagger
 * /api/v1/devices/{id}/status:
 *   get:
 *     tags: [设备管理]
 *     summary: 获取设备状态
 *     description: 获取设备的实时状态信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: string
 *                   enum: [online, offline, fault]
 *                   example: "online"
 *                 lastActiveTime:
 *                   type: string
 *                   format: date-time
 *                 metrics:
 *                   type: object
 *                   description: 设备指标数据
 *                 diagnostics:
 *                   type: object
 *                   description: 设备诊断信息
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 */
router.get('/:id/status', checkAuth, checkPermission(['device:read']), deviceController.getDeviceStatus);

/**
 * @swagger
 * /api/v1/devices/{id}/command:
 *   post:
 *     tags: [设备管理]
 *     summary: 发送设备指令
 *     description: 向设备发送控制指令
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 设备ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - command
 *             properties:
 *               command:
 *                 type: string
 *                 description: 指令类型
 *                 example: "restart"
 *               parameters:
 *                 type: object
 *                 description: 指令参数
 *     responses:
 *       200:
 *         description: 指令发送成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "指令发送成功"
 *                 commandId:
 *                   type: string
 *                   example: "cmd_001"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 设备不存在
 */
router.post('/:id/command', checkAuth, checkPermission(['device:control']), deviceController.sendCommand);

module.exports = router; 